package com.cjlgb.design.upms.service.impl;

import com.cjlgb.design.common.core.bean.ResultBean;
import com.cjlgb.design.common.mybatis.service.impl.BaseServiceImpl;
import com.cjlgb.design.upms.api.entity.SysMenu;
import com.cjlgb.design.upms.mapper.SysMenuMapper;
import com.cjlgb.design.upms.service.SysMenuService;
import com.cjlgb.design.upms.channel.SysMenuChannel;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.time.LocalDateTime;

/**
 * @author WFT
 * @date 2019/7/31
 * description: 系统菜单业务接口实现类
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SysMenuServiceImpl extends BaseServiceImpl<SysMenuMapper,SysMenu> implements SysMenuService {

    private final SysMenuChannel menuChannel;

    @Override
    public ResultBean createMenu(SysMenu menu) {
        //  设置菜单类型
        if (0 != menu.getPid()){
            SysMenu parent = this.getById(menu.getPid());
            Assert.notNull(parent,"父级Id不存在");
            menu.setType(parent.getType() + 1);
            int type = 2;
            if (menu.getType() == type){
                Assert.hasText(menu.getPath(),"菜单路径不能为空");
            }
        } else {
            menu.setType(1);
        }
        //  校验标识是否已存在
        SysMenu dbMenu = this.selectByPermission(menu.getPermission(),null);
        Assert.isNull(dbMenu,"权限标识已存在");
        //  获取当前的菜单的排序值
        Integer sort = this.baseMapper.selectMaxSortByPid(menu.getPid());
        sort = null == sort ? 1 : ++sort;
        menu.setSort(sort);
        //  设置创建时间和修改时间
        LocalDateTime now = LocalDateTime.now();
        menu.setCreateTime(now);
        menu.setLastModifyTime(now);
        //  保存菜单
        if (this.save(menu)){
            //  异步将此菜单授权与系统角色
            Message<SysMenu> message = MessageBuilder.withPayload(menu)
                    .setHeader("method", "add")
                    .build();
            this.menuChannel.output().send(message);
            return ResultBean.ok();
        }
        return ResultBean.error("保存失败");
    }

    @Override
    public ResultBean editMenu(SysMenu menu) {
        boolean updatePermission = false;
        //  判断权限标识是否被修改
        SysMenu dbMenu = this.getById(menu.getId());
        if (!dbMenu.getPermission().equals(menu.getPermission())){
            //  校验标识是否已存在
            Assert.isNull(this.selectByPermission(menu.getPermission(),menu.getId()),"权限标识已存在");
            updatePermission = true;
        }
        menu.setLastModifyTime(LocalDateTime.now());
        //  保存修改
        if (this.updateById(menu)){
            if (updatePermission){
                //  异步更新角色与菜单的关联
                Message<Long> message = MessageBuilder.withPayload(menu.getId())
                        .setHeader("method", "edit")
                        .build();
                this.menuChannel.output().send(message);
            }
            return ResultBean.ok();
        }
        return ResultBean.error("保存失败");
    }

    @Override
    public void deleteById(long id) {
        //  判断菜单下是否有子菜单
        Integer sort = this.baseMapper.selectMaxSortByPid(id);
        Assert.isNull(sort,"请先删除子节点");
        if (this.removeById(id)){
            Message<Long> message = MessageBuilder.withPayload(id)
                    .setHeader("method", "delete")
                    .build();
            this.menuChannel.output().send(message);
        }
    }
}
